package cn.kgc.spring.converters;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 课工场
 * @date 2024/2/20
 * @description  自定义数据类型转换器   String  -> Date
 */
public class MyDateConverter implements Converter<String, Date> {


    private String pattern;

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    //   编写业务代码实现 String -> Date
    @Override
    public Date convert(String source) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        try {
            Date parse = simpleDateFormat.parse(source);
            return  parse;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }


}
